#include "../thread_pool.h"
#include "../user.h"
#include "mysql.h"
/*---------------------delimiter-----------------*/
//从MD5表中选择数据看是否存在,存在返回1
//从MD5表中获取资源文件的路径
//通过硬链接技术，链接到通过秒传技术上传的文件
int select_md5_table(const char* buff, int* length) {
    MYSQL conn;
    MYSQL* pconn = mysql_init(&conn);
    //与MySQL服务器建立连接
    pconn = mysql_real_connect(pconn, "localhost", "root", "1234",
                               "Clouddisk", 0, NULL, 0);
    if (pconn == NULL) {
        printf("%d\n", mysql_errno(&conn));
        exit(1);
    }

    //设置客户端的编码规则，防止出现乱码的情况
    mysql_query(pconn, "set names utf8");

    //初始化MySQL_STMT
    MYSQL_STMT* stmt = mysql_stmt_init(pconn);
    if (stmt == NULL) {
        printf("error:%s\n", mysql_error(pconn));
        mysql_close(pconn);
        exit(1);
    }
    //执行prepare操作，将stmt与模板化的sql语句绑定
    const char* sql = "SELECT filelength FROM md5 WHERE md5 = ?";
    int ret = mysql_stmt_prepare(stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(stmt);
    if (count != 1) {
        printf("(%d, %s\n)", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }
    //设置参数

    char md5[512] = { 0 };
    unsigned long md5_len = 0;

    //绑定参数
    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[0].buffer = md5;
    bind[0].is_null = 0;
    bind[0].length = &md5_len;//指定字符串长度的缓冲区的首地址

    //执行绑定操作
    ret = mysql_stmt_bind_param(stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }

    //传递参数
    strcpy(md5, buff);
    md5_len = strlen(md5);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }
    int res_len;
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数
    //
    res_bind[0].buffer_type = MYSQL_TYPE_LONG;
    res_bind[0].buffer = &res_len;
    res_bind[0].buffer_length = sizeof(int);//确定值,空间要大

    //执行绑定操作
    ret = mysql_stmt_bind_result(stmt, res_bind);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(stmt);
    if (ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(pconn);
        exit(1);
    }

    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        *length = res_len;
    }

    //释放资源
    mysql_stmt_free_result(stmt);
    mysql_close(pconn);
}

